<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="chefan" font-size="40px" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 头部样式结束 -->

        <!-- 主体样式开始 -->
        <el-tree :data="list">
          <!-- 使用默认插槽进行数据的渲染 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="chefan" font-size="30px" /><span>{{
                  data.name
                }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹层 -->
      <el-dialog
        title="新增子部门"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
      <!-- 组件 -->
        <addOrEdit :id="curId"  @success="hSuccess"/>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import addOrEdit from '@/views/departments/deptDialog.vue'
import { getDepartments } from '@/api/departments'
import { transTreeList } from '@/utils/index'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      list: [],
      showDialog: false,
      // 父级的id
      curId: ''
    }
  },
  created() {
    // 获取部门列表
    this.loadgetDepartments()
  },
  methods: {
    hSuccess() {
      // 关闭弹层，刷新列表
      this.showDialog = false
      this.loadgetDepartments()
    },
    // 获取部门列表
    async loadgetDepartments() {
      try {
        const res = await getDepartments()
        console.log(res, 'getDepartments')
        // 保存数据
        // 将平铺数据转化成树形数据
        this.list = transTreeList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    hAdd(id) {
      this.curId = id
      this.showDialog = true
    }
  }
}
</script>
